package com.dingding.common.core.util.tools.wechat.pay;

import com.dingding.common.core.config.WeChatConfig;
import com.dingding.common.core.util.MD5Util;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.common.core.util.tools.wechat.pay.paybean.request.WechatPayOrderQueryRequest;
import com.dingding.common.core.util.tools.wechat.pay.paybean.request.WechatPayOrderRequest;
import com.dingding.common.core.util.tools.wechat.pay.paybean.request.WechatPayRequest;

/**
 * @Description: 微信支付相关请求创建工厂
 * @author fengshuonan
 * @date 2016年6月4日 上午9:33:24
 */
public class WechatPayReqFactory {

	/**
	 * @Description:创建微信支付请求
	 * @author fengshuonan
	 * @type 支付类型  1：私家车支付 2：出租车支付 3:专车支付
	 * @param <T>
	 * @return Object
	 */
	@SuppressWarnings("unchecked")
	public static <T> T createRequest(Class<?> clazz, int type) {

		WechatPayRequest payRequest = null;

		if (clazz.equals(WechatPayOrderRequest.class)) {
			payRequest = createAppPayRequest(type);
			return (T) payRequest;
		} else if (clazz.equals(WechatPayOrderQueryRequest.class)) {
			payRequest = createWechatPayQueryRequest();
			return (T) payRequest;
		}

		return null;
	}

	/**
	 * @Title: createAppPayRequest
	 * @Description: 创建一个app微信支付请求bean
	 * @author fengshuonan
	 * @return WechatPayOrderRequest
	 * @throws
	 */
	private static WechatPayOrderRequest createAppPayRequest(int type) {

		WechatPayOrderRequest payRequest = new WechatPayOrderRequest();

		payRequest.setAppid("wx8b6d01f28792414a");// 叮叮搭车应用的appid
		payRequest.setMch_id("1316301901");// 叮叮搭车应用商户号
		payRequest.setNonce_str(ToolUtil.randomLenStr(20));// 生成20位随机码
		payRequest.setBody("dingdingdache");// 简要描述
		if (type == 1) { // 私家车支付
			payRequest.setNotify_url(WeChatConfig.getWECHAT_NOTIFY_URL());// 微信响应服务器的地址
		} else if(type == 2){ // 出租车支付
			payRequest.setNotify_url(WeChatConfig.getTXT_WECHAT_NOTIFY_URL());// 微信响应服务器的地址
		} else if(type == 3){//专车支付
			payRequest.setNotify_url(WeChatConfig.getSPECIAL_WECHAT_NOTIFY_URL());// 微信响应服务器的地址
		}
		
		System.out.println("微信回调地址：" + payRequest.getNotify_url());
		
		payRequest.setTrade_type("APP");// 交易类型 app方式

		return payRequest;
	}

	/**
	 * @Title: createWechatPayQueryRequest
	 * @Description:创建微信支付查询支付结果请求bean
	 * @author fengshuonan
	 * @return WechatPayOrderQueryRequest
	 * @throws
	 */
	private static WechatPayOrderQueryRequest createWechatPayQueryRequest() {

		WechatPayOrderQueryRequest request = new WechatPayOrderQueryRequest();
		request.setAppid("wx8b6d01f28792414a");// 叮叮搭车应用的appid
		request.setMch_id("1316301901");// 叮叮搭车应用商户号
		request.setNonce_str(ToolUtil.randomLenStr(20));// 生成20位随机码

		String apiSec = "j5axbIfMSZjsiEGAScGMCUhp5kO2Oqaj";// API安全秘钥
		String stringSignTemp = ToolUtil.lexSortBean(request, WechatPayOrderQueryRequest.class) + "key=" + apiSec;
		String sign = MD5Util.md5Encrypt(stringSignTemp).toUpperCase();
		request.setSign(sign);

		return request;
	}
}
